<?php

// 红包充值规则

namespace app\console\controller;

use think\Url;
use think\Request;
use think\Controller;
use app\console\model\RedPacketRule as RedPacketRuleModel;

class RedPacketRule extends Controller
{

    /**
     * 红包充值规则列表
     * @param Request $request
     * @param RedPacketRuleModel $packetRule
     * @return mixed
     */
    public function index(Request $request, RedPacketRuleModel $packetRule)
    {

        if ($request->instance()->isPost()) {

            if ($request->instance()->param('name')) {

                // 提交数据
                $packetRule->allowField(true)->save($request->instance()->param());

            }

            $ids = $request->instance()->param('ids/a');
            $name = $request->instance()->param('MemberName/a');
            $expA = $request->instance()->param('expA/a');
            $expB = $request->instance()->param('expB/a');
            $actualPrice = $request->instance()->param('ActualPrice/a');
            $virtualPrice = $request->instance()->param('VirtualPrice/a');
            $duration = $request->instance()->param('Durations/a');

            for ($i = 0; $i < count($ids); $i++) {
                $ini = [
                    'name'          => $name[$i],
                    'exp_a'         => $expA[$i],
                    'exp_b'         => $expB[$i],
                    'actual_price'  => $actualPrice[$i],
                    'virtual_price' => $virtualPrice[$i],
                    'duration'      => $duration[$i]

                ];

                $packetRule->where('id', $ids[$i])->update($ini);
            }

        }

        $data = $packetRule->order('id', 'asc')->select();


        return $this->fetch('index', [
            "mate_title" => '红包充值规则列表',
            'data'       => $data
        ]);

    }



    public function destroy($id,RedPacketRuleModel $packetRule)
    {

        if ($packetRule->destroy($id)) {
            win('删除成功', Url::build('console/RedPacketRule/index'));
        }

        fail('删除失败');

    }
}